package com.iwhalecloud.bss.kite.cucc.service.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.biz.IBizService;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IReceiptService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.FlowValueInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.ReceiptInfoVO;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.CuccProdAttrExtCache;
import com.ztesoft.bss.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author yangwj
 * @date 2020/5/8 9:27
 */
@Service
public class ReceiptService implements IReceiptService {
    @Autowired
    private IBizService bizService;

    @Override
    public ReceiptInfoVO getReceiptInfo(String orderId, String taskId) {
        Map<String, Object> dataMap = this.bizService.getBizServiceList(orderId);
        JSONObject dataJson = JSONObject.parseObject(JSONObject.toJSONString(dataMap));

        ReceiptInfoVO receiptInfoVO = null;

        //主商品回执
        JSONArray array = (JSONArray) JSONPath.eval(dataJson, "$.mainServiceList.flowData");
        receiptInfoVO = this.getReceiptInfoVO(array, taskId);
        if (!KiteObjectUtils.isEmpty(receiptInfoVO)) {
            return receiptInfoVO;
        }

        //子商品回执
        JSONArray subServiceArray = dataJson.getJSONArray("subServiceList");
        Iterator it = subServiceArray.iterator();
        while (it.hasNext()) {
            JSONObject subFlowData = (JSONObject) it.next();
            JSONArray subArray = (JSONArray) JSONPath.eval(subFlowData, "$.flowData");
            receiptInfoVO = this.getReceiptInfoVO(subArray, taskId);
            if (!KiteObjectUtils.isEmpty(receiptInfoVO)) {
                return receiptInfoVO;
            }
        }
        return receiptInfoVO;
    }

    /**
     * 获取ReceiptInfoVO对象
     *
     * @param array
     * @param taskId
     */
    private ReceiptInfoVO getReceiptInfoVO(JSONArray array, String taskId) {
        Iterator its = array.iterator();
        while (its.hasNext()) {
            JSONObject flowData = (JSONObject) its.next();
            if (!StringUtil.equals(flowData.getString("taskId"), taskId)) {
                continue;
            }
            ReceiptInfoVO receiptInfoVO = new ReceiptInfoVO();
            List<FlowValueInfoVO> flowValueInfos = new ArrayList<>();
            String flowValueStr = flowData.getString("flowValue");
            JSONArray flowValueArray = JSONArray.parseArray(flowValueStr);

            flowValueArray.stream().forEach(flowValue -> {
                FlowValueInfoVO valueInfoVO = JSONObject.toJavaObject((JSON) flowValue, FlowValueInfoVO.class);
                if (KiteListUtils.isNotEmpty(CuccProdAttrExtCache.get(valueInfoVO.getCode(), "", ""))) {
                    valueInfoVO.setcName(CuccProdAttrExtCache.get(valueInfoVO.getCode(), "", "").get(0).getCname());
                } else {
                    valueInfoVO.setcName(valueInfoVO.getCode());
                }
                flowValueInfos.add(valueInfoVO);
            });
            Map<String, List<FlowValueInfoVO>> collect = flowValueInfos.stream().collect(Collectors.groupingBy(FlowValueInfoVO::getGroupId));

            receiptInfoVO.setFlowValuesGroup(collect);
            receiptInfoVO.setFlowKey(flowData.getString("flowKey"));
            receiptInfoVO.setTaskId(flowData.getString("taskId"));
            receiptInfoVO.setOrderId(flowData.getString("orderId"));
            receiptInfoVO.setFlowDataId(flowData.getString("flowDataId"));
            return receiptInfoVO;
        }
        return null;
    }


}
